const app = getApp()
Page({

  ...app.globalData.function,
  data: {
    ...app.globalData.config,
    storageInfo:'',
    currentTab:1,
    currentAll:1,
    new_goods:[],
    winHeight: "",// 窗口高度
    windowHeight: '',
    windowWidth: '',
    stock_sale:'asc',
    pricle_sale:'asc',
    coupon_list:[],
    is_collect:0,
    userInfo:"",
    banner_list:[],
    recomgoods_list:[],//厂家推荐商品
    image:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {    
    if(options.storageId){
      getApp().storageId=options.storageId
    }

    if(options && options.q && options.q != "undefined"){
      let scene = decodeURIComponent(options.q)
      let temp = this.getwxUrlParam(scene)
      if(temp && temp.memberId){
        getApp().storageId=temp.storageId
      }
    }


    let that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        let clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        let calc = (clientHeight - 50 - 54) * rpxR - 130;
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          winHeight: calc,
          image:that.data.api_url+"/static/upload/new_header.png"
        });
      }
    });
  },





  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  
    if(this.getStorage('user_info')){      
      this.setData({
        userInfo: this.getStorage('user_info')
      })
    }
     this.setData({
       new_goods:[]
     })
     this._requestPageList()
        // this.setData({
    //   userInfo:this.getStorage('user_info')
    // })

  },


    // 点击标题切换当前页时改变样式
    changeNav: function (e) {
      let currentIndex = e.target.dataset.current;
      if (this.data.currentTaB == currentIndex) {      
        return false;
      } else {
        this.setData({
          pageNo:1,
          currentTab: currentIndex
        })
      }
      this.setData({
        new_goods:[]
      });
      this._requestPageList()
    },





    changeNavAll: function (e) {
      let currentIndex = e.target.dataset.currentall;
      this.setData({
        pageNo:1,
        new_goods:[],
        currentAll: currentIndex
      });

      if(currentIndex==2){
        if(this.data.stock_sale=='asc'){
          this.setData({stock_sale:'desc'})
        }else{
          this.setData({stock_sale:'asc'})
        }
      }
      if(currentIndex==3){
         console.log(this.data.pricle_sale)
        if(this.data.pricle_sale=='asc'){
          this.setData({pricle_sale:'desc'})
        }else if(this.data.pricle_sale=='desc'){
           this.setData({pricle_sale:'asc'})
        }
      }
      this._requestPageList(true);
    },


     // 参数
  _requestPageList(isLoadMore = false, callback) {
    // isLoadMore 为true是上拉加载，不需要置空数组
    if(!isLoadMore){
      this.setData({
        allTabList:[], // 全部       
        pageNo: 1
      });
    }

    let opt = {
      storageId :getApp().storageId,  
      currentTab:this.data.currentTab   
    }

    let token=this.getStorage('user_info').wx_openid
    if(token){
      opt.token=token
    }

    if(this.data.currentTab==2){
      opt.currentAll=this.data.currentAll
      if(this.data.currentAll==2) opt.sort=this.data.stock_sale
      if(this.data.currentAll==3) opt.sort=this.data.pricle_sale
    } 


    
    this._requestPageListCom("virtualTaskList", opt, callback);
  },
  // 列表接口
  _requestPageListCom(url, params, callback) {    
    this.ShopInfo(params).then(res => {        
      if (res.resCode == "9999") {            
        this.setData({
          storageInfo:res.data.header, 
          is_collect:res.data.header.is_collect,
          coupon_list:res.data.coupon_list,
          banner_list:res.data.banner_list,
          recomgoods_list:res.data.recomgoods_list       
        })
        if(this.data.currentTab==1 || this.data.currentTab==2 || this.data.currentTab==3){            
          let oldList = [];     
          oldList = this.data.new_goods;
          const newGoodsList = res.data.new_goods.lists
          const allData = [...oldList, ...newGoodsList]
          this.setData({
            recordCount: res.data.new_goods.page,
            new_goods:allData
          })
          if(callback && typeof callback === 'function'){
            callback()
          }
        }
      }      
    })
  },

   // 上拉加载  滚动到底部/右边时触发
   allLoadMore(e){
    let _self = this;
    if (this._allLoadMore  || _self.data.recordCount === 0) return
    this._allLoadMore = true;

    this.setData({
      pageNo: ++_self.data.pageNo
    });
    this._requestPageList(true, () => {
      _self._allLoadMore = false;
    });
  },
  // 下拉刷新接口
  onAllRefresh() {
    if (this._allFreshing) return
    this._allFreshing = true
    this.setData({new_goods:[]})
    this._requestPageList(false, () => {
      // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新
      this.setData({
        triggered: false
      })
      this._allFreshing = false
    });
  },
  pScrollTo(){
    // 返回顶部
    this.setData({
      scrollTop:0
    });
  },

  // 详情
  goDetails:function(events){
    let meetId = events.currentTarget.dataset.posid;
      this.router("/pagesShop/detail/index?id="+meetId)
  },


  // 获取滚动距离
  onPageScroll(e) {
    // 获取滚动距离
    let _this = this;
    let Top = e.scrollTop
    // 将滚动距离(位移)动态添给滚动条的Top
    _this.setData({
      scrollTop: Top
    })
  },

  draw_coupon(events){
    let storageData = this.getStorage('user_info')
    let that=this
    let obj_data={
      token:storageData.wx_openid,
      couponId:events.currentTarget.dataset.couponid,
    }

    this.drawCoupon(obj_data).then(res=>{          
      if(res.resCode=="9999"){
        wx.showToast({title: res.resMessage,icon:"none"})
      }else{
        wx.showToast({title: res.resMessage,icon:"none"})
      } 
      this.selectComponent("#popup").hide()         
    })
  },

   //收藏信息
   collect(){
    let that=this
    var storageData = this.getStorage('user_info');
    let storageId= getApp().storageId
    this.getCollect({token:storageData.wx_openid,storageId:storageId}).then(res=>{
        if(res.resCode=='9999'){
          if(res.resMessage=='收藏成功'){           
            that.setData({is_collect:1})
            wx.showToast({title: res.resMessage,icon:"none"})
          }else if(res.resMessage=='取消收藏'){
            that.setData({is_collect:0})
            wx.showToast({title: res.resMessage,icon:"none"})
          }else{
            wx.showToast({title: res.resMessage,icon:"none"})
          }
        }
     }) 
    },

    handleSwiperClick(e){
      let path=e.detail.path
      if(path){
        this.router(path)
      }
    },
    getwxUrlParam(url) {
      let theRequest = {};
      if(url.indexOf("#") != -1){
        const str=url.split("#")[1];
        const strs=str.split("&");
        for (let i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
      }else if(url.indexOf("?") != -1){
        const str=url.split("?")[1];
        const strs=str.split("&");
        for (let i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
      }
      return theRequest;
    },

    


 

})